﻿#ifndef _tcp_client_20220407_
#define _tcp_client_20220407_

#include <string>
#include <memory>
#include <atomic>
#include <mutex>
#include <SLT_tool_pub.h>

////event id
//#define EPOLL_LIST_CMD_ADD_LISTEN_SOCKET        1
//#define EPOLL_LIST_CMD_CONNECT                  2 
//#define EPOLL_LIST_ADD_CONNECT                  3
//#define EPOLL_LIST_SHUTDOWNFLG                  4
//#define EPOLL_LIST_SOCKET_DEL                   6
//#define EPOLL_LIST_SOCKET_STARTRECV             7
//#define EPOLL_LIST_SOCKET_RECVDATAONE           8
//#define EPOLL_LIST_SOCKET_CHECKTIMEOUT          9   //检测tcpclient超时
//
//#define SHUTDOWNFLG                 1


enum tcp_error_type
{
    tcp_error_type_ok,
    tcp_error_type_err,
    tcp_error_type_localclose,
    tcp_error_type_remoteclose,
    tcp_error_type_timeout,
};

#define tcp_send_error_again -11

class slIpAddr
{
public:
	int m_ip = 0;
	int m_port = 0;
};

class SLTcpClientCB
{
public:
    virtual void TcpClient_onConnect(tcp_error_type _errType) = 0;
    virtual void TcpClient_onRecv(char* _pdata, int _len) = 0;
    virtual void TcpClient_onSend() = 0;
    virtual void TcpClient_onError(tcp_error_type _type) = 0;
};
typedef std::shared_ptr< SLTcpClientCB>   SLTcpClientCBPtr;

class SLTcpClient
{
public:
    virtual void bindCB(SLTcpClientCBPtr _pcb) = 0;
    virtual void connect2(std::string _serIp, int _serPort, int _timeout = 5000, std::string _lip = "", int _lport = 0) = 0;
    virtual void close2() = 0;
    //返回值表示发送的字节数，返回小于0失败。需要判断返回值：tcp_send_error_again - 需要重新发送数据
	virtual int  send2(const char* _pdata, int _dataLen) = 0;
	virtual void recv2() = 0;

    virtual void setRecv(bool _enable) = 0;
    virtual bool getRecv() = 0;
    virtual int  getRecvCount() = 0;
    
    virtual const slIpAddr& getLocalAddr() = 0;
    virtual const slIpAddr& getRemoteAddr() = 0;
};
typedef std::shared_ptr<SLTcpClient>   SLTcpClientPtr;



#endif